package com.shun.upms.action;


import com.shun.common.R;
import com.shun.common.RC;
import com.shun.common.log.LogType;
import com.shun.common.log.annotation.ShunLog;
import com.shun.upms.dto.SysAdminRoleDto;
import com.shun.upms.dto.SysRoleMenuDto;
import com.shun.upms.service.ISysAdminRoleService;
import com.shun.upms.vo.SysAdminRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统管理角色表 前端控制器
 * </p>
 *
 * @author clever
 * @since 2021-09-27
 */
@Slf4j
@RestController
@RequestMapping("/upms/role")
@Api(tags = "角色的API")
public class SysAdminRoleAction {

    @Autowired
    private ISysAdminRoleService roleService;

    /**
     * 新增角色
     * @return
     */
    @PostMapping("/v1/addAdminRole")
    @ApiOperation(value = "新增角色")
    @ShunLog(title = "角色",logType = LogType.INSERT)
    public R addAdminRole(@RequestBody @Validated SysAdminRoleDto dto){
        log.debug("SysAdminRoleDto:{}",dto);
        return roleService.addAminRole(dto);
    }

    /**
     * 编辑角色
     * @return
     */
    @PostMapping("/v1/editAdminRole")
    @ApiOperation(value = "修改角色")
    @ShunLog(title = "角色",logType = LogType.UPDATE)
    public R editAdminRole(@RequestBody @Validated SysAdminRoleDto dto){
        log.debug("SysAdminRoleDto:{}",dto);
        return roleService.editAdminRole(dto);
    }

    /**
     * 删除角色
     * @return
     */
    @GetMapping("/v1/delAdminRole/{roleId}")
    @ApiOperation(value = "删除角色")
    @ShunLog(title = "角色",logType = LogType.DELETE)
    public R delAdminRole(@PathVariable("roleId") String roleId){
        log.debug("SysAdminRoleId:{}",roleId);
        return roleService.delRole(roleId);
    }

    /**
     * 角色详细信息，根据roleId
     * @return
     */
    @PostMapping("/v1/getAdminRole/{roleId}")
    @ApiOperation(value = "查询角色详细")
    public R getAdminRole(@PathVariable("roleId") String roleId){
        log.debug("SysAdminRoleId:{}",roleId);
        SysAdminRole role = roleService.getById(roleId);
        return (role != null)?R.suc(role): RC.FEIGN_FAIL.toR("没有角色信息！");
    }

    /**
     * 获取角色分页信息
     * @return
     */
    @PostMapping("/v1/pageAdminRole")
    @ApiOperation(value = "角色分页")
    public R pageAdminRole(@RequestBody SysAdminRoleDto dto){
        return roleService.pageAdminRole(dto);
    }

    /**
     * 获取角色名称和ID的信息
     * @return
     */
    @PostMapping("/v1/getNameAndId")
    @ApiOperation(value = "获取角色名称和ID的信息")
    public R getNameAndId(){
        log.debug("获取角色名称和ID的列表信息！");
        return roleService.getRoleNameAndId();
    }

    /**
     * 编辑角色的菜单列表
     * @return
     */
    @PostMapping("/v1/editRoleMenus")
    @ApiOperation(value = "更新角色的菜单ID")
    public R editRoleMenus(@RequestBody SysRoleMenuDto dto){
        log.debug("dto:{}",dto);
        return roleService.editRoleMenu(dto);
    }

    /**
     *
     * @return
     */
    @GetMapping("/v1/setRoleUsers")
    @ApiOperation(value = "设置角色下的用户列表")
    @ShunLog(title = "角色",logType = LogType.OTHER)
    public R setRoleUsers(String roleId,String userIds){
        log.debug("roleId:{},userID:{}",roleId,userIds);
        return roleService.editRoleUser(roleId,userIds.split(","));
    }
}